﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GSync
{
    public partial class ImageButton : UserControl
    {
        public ImageButton()
        {
            InitializeComponent();
            
            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            SetFrame(BorderWidth, e.Graphics, BorderColor);
            
        }
        [EditorBrowsable(EditorBrowsableState.Always)]
        public Image Image
        {
            get
            {
                return ImagePictureBox.Image;
            }
            set
            {
                ImagePictureBox.Image = value;
            }
        }

 
        [EditorBrowsable( EditorBrowsableState.Always )]
        public string Caption
        {

            get {
                return CaptionLabel.Text;
            }
            set {
                CaptionLabel.Text = value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Always)]
        public Color BorderColor { get; set; }
        [EditorBrowsable(EditorBrowsableState.Always)]
        public Color PressedBorderColor { get; set; }
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int BorderWidth { get; set; }
        [EditorBrowsable(EditorBrowsableState.Always)]
        public int PressedBorderWidth { get; set; }


        protected void SetFrame(int width, Graphics graphics, Color color)
        {
            int eraserWidth = Math.Max(BorderWidth, PressedBorderWidth);
            int controlWidth = this.Width - 1;
            int controlHeight = this.Height - 1;
            graphics.DrawRectangle(new Pen(this.BackColor, eraserWidth), new Rectangle(0, 0, controlWidth, controlHeight));
            graphics.DrawRectangle(new Pen(color, width), new Rectangle(0, 0, controlWidth, controlHeight));
        }

        private void ImageButton_MouseDown(object sender, MouseEventArgs e)
        {
            SetFrame(PressedBorderWidth, this.CreateGraphics(), PressedBorderColor);
        }

        private void ImageButton_MouseUp(object sender, MouseEventArgs e)
        {
            if( !IsDisposed )
                SetFrame(BorderWidth, this.CreateGraphics(), BorderColor);
        }

        private void ImagePictureBox_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }

        
    }
}
